﻿#include "ActionInitialization.hh"

#include "DetectorConstruction.hh"
#include "LETdMessenger.hh"
#include "MyMacros.hh"
#include "PrimaryGeneratorAction.hh"
#include "RgProjScorerMsgr.hh"
#include "RunAction.hh"
#include "Util.hh"

using std::make_unique;

ActionInitialization::ActionInitialization(DetectorConstruction* det) : _fDetector(det)
{
    MYG4TID(tid);
    DINFOLN("```````````````````````````````````[tid {} ActionInitialization/ActionInitialization]", tid);
}

ActionInitialization::~ActionInitialization()
{
    Util::Clear(_fLETdMsgrWorker);
    Util::Clear(_rangeProjMsgrWorker);
}

void ActionInitialization::BuildForMaster() const
{
    MYG4TID(tid);
    DINFOLN("```````````````````````````````````[tid {} ActionInitialization] BuildForMaster()", tid);

    RunAction* runAct = new RunAction();
    SetUserAction(runAct);

    // 在 master 上注册 LETdMessenger
    if (!_fLETdMsgrMaster)
    {
        _fLETdMsgrMaster = make_unique<LETdMessenger>();
        //DINFOLN("```````````````````````````````````[tid {} ActionInitialization/BuildForMaster] LETdMessenger instantiated on Master", tid);
    }

    if (!_rangeProjMsgrMaster)
    {
        //DINFOLN("```````````````````````````````````[tid {} ActionInitialization/BuildForMaster] RgProjScorerMsgr instantiated on Master", tid);
        _rangeProjMsgrMaster = make_unique<RgProjScorerMsgr>();
    }
}

void ActionInitialization::Build() const
{
    MYG4TID(tid);
    //DINFOLN("```[tid {} ActionInitialization] Build, Set Primary Generator Action", tid);

    auto* const prim = new PrimaryGeneratorAction(_fDetector);
    SetUserAction(prim);

    auto* const runAct = new RunAction();
    SetUserAction(runAct);

#if 10
    /// 在 worker 上注册 LETdMessenger
    auto* const pLet = new LETdMessenger();
    _fLETdMsgrWorker.push_back(pLet);
    //DINFOLN("```````````````````````````````````[tid {} ActionInitialization/Build] LETdMessenger= {} on Worker", tid, fmt::ptr(pLet));

#endif

#if 10
    auto pRgPrj = new RgProjScorerMsgr();
    //DINFOLN("```````````````````````````````````[tid {} ActionInitialization/Build] RgProjScorerMsgr {} on Worker", tid, fmt::ptr(pRgPrj));
    _rangeProjMsgrWorker.push_back(pRgPrj);
#endif
}
